﻿using Shake.Framework.Infrastructure;
using Shake.Framework.Utility;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace Shake.Framework.Setup
{
    public class FeatureInitiliazer
    {
        public static void Initialize()
        {
            var initializers = ReflectUtil.GetClassByInterface<IInitializer>();
            var classes = Assembly.GetExecutingAssembly().ExportedTypes.Where(t => t.GetInterface(typeof(IInitializer).Name) != null).ToList();
            initializers.AddRange(classes);

            foreach (var initializer in initializers)
            {
                Task.Run(() =>
                {
                    try
                    {

                        var instance = initializer.Assembly.CreateInstance(initializer.FullName) as IInitializer;
                        instance.Initialize();
                    }
                    catch (Exception ex)
                    {
                        NLogUtil.Warn(ex, initializer.FullName + " Initialize初始化执行错误");
                    }
                });
            }
        }
    }
}
